package com.chucang.shucang.oms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chucang.shucang.common.base.constant.SecurityConstant;
import com.chucang.shucang.common.base.exception.FastException;
import com.chucang.shucang.common.base.utils.OpsUtil;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.mq.to.BlindBoxOrderBaseTO;
import com.chucang.shucang.common.security.annotation.Inner;
import com.chucang.shucang.oms.constant.OmsCommonConstant;
import com.chucang.shucang.oms.entity.BlindBoxOrderBaseEntity;
import com.chucang.shucang.oms.exception.OmsErrorCode;
import com.chucang.shucang.oms.feign.PmsService;
import com.chucang.shucang.oms.feign.vo.BlindBoxBaseEntity;
import com.chucang.shucang.oms.service.BlindBoxOrderBaseService;
import com.chucang.shucang.oms.vo.BlindBoxOrderBaseResVO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 盲盒基础订单表
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-12
 */
@RestController
@RequestMapping("/blindboxorderbase")
@RequiredArgsConstructor
@Api(tags = "盲盒基础订单表")
public class BlindBoxOrderBaseController {

    private final BlindBoxOrderBaseService blindBoxOrderBaseService;
    private final PmsService pmsService;

    /**
     * 检查订单信息
     *
     * @param orderSn
     * @return
     */
    @Inner(value = false)
    @GetMapping("/checkBlindBoxOrder/{orderSn}")
    public R<BlindBoxOrderBaseResVO> checkBlindBoxOrder(@PathVariable String orderSn) {
        LambdaQueryWrapper<BlindBoxOrderBaseEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BlindBoxOrderBaseEntity::getOrderSn, orderSn)
                .select();
        BlindBoxOrderBaseEntity one = blindBoxOrderBaseService.getOne(lambdaQueryWrapper);
        BlindBoxOrderBaseResVO blindBoxOrderBaseResVO = new BlindBoxOrderBaseResVO();
        BeanUtil.copyProperties(one, blindBoxOrderBaseResVO);
        //藏品信息
        R<BlindBoxBaseEntity> baseInfo = pmsService.getBlindBoxBaseInfo(one.getBbId(), SecurityConstant.FROM_IN);
        BlindBoxBaseEntity baseEntity = OpsUtil.of(baseInfo)
                .assertDataNotNull(e -> new FastException("查询藏品信息错误！"))
                .getData()
                .get();
        blindBoxOrderBaseResVO.setCoverPicture(baseEntity.getCoverPicture());
        blindBoxOrderBaseResVO.setBbName(baseEntity.getBbName());
        return R.ok(blindBoxOrderBaseResVO);
    }

    /**
     * 取消订单
     *
     * @param orderSn
     * @return
     */
    @GetMapping("/cancelBlindBoxOrder/{orderSn}")
    public R<String> cancelBlindBoxOrder(@PathVariable String orderSn) {
        if (!blindBoxOrderBaseService.cancelBlindBoxOrder(orderSn)) {
            return R.failed(OmsErrorCode.ORDER_CAN_NOT_CANCEL.getCode(), OmsErrorCode.ORDER_CAN_NOT_CANCEL.getMsg());
        }
        return R.ok();
    }

    @Inner
    @PostMapping("/createBlindBoxOrder")
    public R<String> createBlindBoxOrder(@RequestBody BlindBoxOrderBaseTO blindBoxOrderBase) {
        //处理盲盒订单
        BlindBoxOrderBaseEntity orderBaseEntity = new BlindBoxOrderBaseEntity();
        BeanUtil.copyProperties(blindBoxOrderBase, orderBaseEntity);
        blindBoxOrderBaseService.save(orderBaseEntity);
        orderBaseEntity.setStatus(OmsCommonConstant.WAITING_FOR_PAY);
        return R.ok();
    }

    @GetMapping("/cancelBlindBoxNormalOrder/{orderSn}")
    public R<String> cancelBlindBoxNormalOrder(@PathVariable String orderSn) {
        if (!blindBoxOrderBaseService.cancelBlindBoxNormalOrder(orderSn)) {
            return R.failed(OmsErrorCode.ORDER_CAN_NOT_CANCEL.getCode(), OmsErrorCode.ORDER_CAN_NOT_CANCEL.getMsg());
        }
        return R.ok();
    }

    @Inner
    @GetMapping("/blindBoxOrderSuccessByAliPay/{orderSn}")
    public R<String> blindBoxOrderSuccessByAliPay(@PathVariable String orderSn) {
        LambdaUpdateWrapper<BlindBoxOrderBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BlindBoxOrderBaseEntity::getOrderSn, orderSn)
                .set(BlindBoxOrderBaseEntity::getStatus, 3)
                .set(BlindBoxOrderBaseEntity::getPayType, 1);
        blindBoxOrderBaseService.update(updateWrapper);
        return R.ok();
    }

    @Inner
    @GetMapping("/blindBoxOrderSuccessByWallet/{orderSn}")
    public R<String> blindBoxOrderSuccessByWallet(@PathVariable String orderSn) {
        LambdaUpdateWrapper<BlindBoxOrderBaseEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BlindBoxOrderBaseEntity::getOrderSn, orderSn)
                .set(BlindBoxOrderBaseEntity::getStatus, 3)
                .set(BlindBoxOrderBaseEntity::getPayType, 0);
        blindBoxOrderBaseService.update(updateWrapper);
        return R.ok();
    }

}